﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using wpf_read_config;
using mywinform.ui;
namespace mywinform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            QueryAllCourse();
        }

        private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        //窗体加载事件
        private void Form1_Load(object sender, EventArgs e)
        {
            //调用查询全部课程的方法
            QueryAllCourse();
            //ConfigLoad cl= new ConfigLoad("config.txt");
            //ConfigSet cs = new ConfigSet("config.txt");
            //cs.SetConfigValue("name_1", "电表1");
            //cs.WriteConfigToFile(ConfigFile.appendFile);

        }
        //查询全部课程
        private void QueryAllCourse()
        {
            //数据库连接串
            string connStr = "Data Source=.;Initial Catalog=hohd;User ID=sa;Password=123456";
            //创建SqlConnection的实例
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(connStr);
                //打开数据库
                conn.Open();
                string sql = "select * from st_power";
                //创建SqlDataAdapter类的对象
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                //创建DataSet类的对象
                DataSet ds = new DataSet();
                //使用SqlDataAdapter对象sda将查新结果填充到DataSet对象ds中
                sda.Fill(ds);
                //设置表格控件的DataSource属性
                dataGridView1.DataSource = ds.Tables[0];
                //设置数据表格上显示的列标题
                dataGridView1.Columns[0].HeaderText = "序号";
                dataGridView1.Columns[1].HeaderText = "编号";
                dataGridView1.Columns[2].HeaderText = "时间";
                dataGridView1.Columns[3].HeaderText = "电量";
                dataGridView1.Columns[2].Width = 120;
                //设置数据表格为只读
                dataGridView1.ReadOnly = true;
                //不允许添加行
                dataGridView1.AllowUserToAddRows = false;
                //背景为白色
                dataGridView1.BackgroundColor = Color.White;
                //只允许选中单行
                dataGridView1.MultiSelect = false;
                //整行选中
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询错误！" + ex.Message);
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            PowerNameSetting pns = new PowerNameSetting();
            pns.ShowDialog();
            
        }
    }
}
